package com.zl.open_app;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

import android.util.Log;
import android.app.ActivityManager;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.ComponentName;
import android.content.Intent;
import android.content.Context;

public class app_list
{
	private static String TAG = "app_list";
	
	public String getCurrentActivityName(Context context) {  
		  ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);  
		  
		  
		  // get the info from the currently running task  
		  List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);  
		  
		  
		  ComponentName componentInfo = taskInfo.get(0).topActivity;  
		  return componentInfo.getClassName();  
		} 

	public static List<open_app> getAllApps(Context context,String app_name,String [] package_name,String [] class_name) {

		List<open_app> allAppInfos = new ArrayList<open_app>();
		PackageManager pm = context.getPackageManager();
		Intent main = new Intent(Intent.ACTION_MAIN, null);
		main.addCategory(Intent.CATEGORY_LAUNCHER);
		final List<ResolveInfo> infos = pm.queryIntentActivities(main, 0);
		Collections.sort(infos, new ResolveInfo.DisplayNameComparator(pm));
		if (infos == null || infos.size() <= 0) {
			return null;
		}
		for (int i = 0; i < infos.size(); i++) {
			ResolveInfo info = infos.get(i);
			if (info.activityInfo.packageName.equals("com.dx.launcher_")
					|| info.activityInfo.packageName
							.equals("com.android.contacts")
					|| info.activityInfo.packageName
							.equals("com.pg.software.services")
					|| info.activityInfo.packageName
							.equals("com.pg.software.launcher")) {
				continue;
			} else {
				open_app appInfo = new open_app();
				appInfo.setAppName(info.loadLabel(pm).toString());
				appInfo.setIcon(info.loadIcon(pm));
				appInfo.setPkgName(info.activityInfo.packageName);
				appInfo.setProcName(info.activityInfo.name);
				//allAppInfos.add(appInfo);
				
				if(info.loadLabel(pm).toString().equals(app_name))
				{
					package_name[0] = info.activityInfo.packageName;
					class_name[0]   = info.activityInfo.name;
					break;
				}
			}
		}
		
		return allAppInfos;
	}
}